package pers.cz.flowcell;

import org.dom4j.Document;
import org.dom4j.Element;
import pers.cz.mxgraph.MxCell;
import pers.cz.mxgraph.MxEdge;
import pers.cz.mxgraph.MxObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jef-flow-spring-boot-starter
 * @description: 抽象xml大类解析
 * @author: Cheng Zhi
 * @create: 2023-02-12 17:32
 **/
public abstract class AbstractJefFlowRoot implements JefFlowRoot {

    private List<JefFlowCell> cells = new ArrayList<>();

    public Map<Integer, JefFlowCell> cellsMap = new HashMap<Integer, JefFlowCell>();

    public AbstractJefFlowRoot(Document document) {
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        readElement(elements);
    }

    public List<JefFlowCell> getCells() {
        return this.cells;
    }

    public void addCell(JefFlowCell jefFlowCell) {
        this.cells.add(jefFlowCell);
        cellsMap.put(jefFlowCell.getId(), jefFlowCell);
    }

    public JefFlowCell getCellById(Integer id) {
        return cellsMap.get(id);
    }
    /**
     * 迭代处理xml中的元素
     * @param elements
     */
    @Override
    public void readElement(List<Element> elements) {

        for (Element element : elements) {
            handleElement(element);
            if (element.elements().size() > 0) {
                readElement(element.elements());
            }
        }
    }


    public abstract void handleElement(Element element);

}
